package com.gtm.controller;

import com.gtm.model.Assignment;
import com.gtm.model.Review;
import com.gtm.repository.AssignmentRepository;
import com.gtm.service.AssignmentService;
import com.mongodb.gridfs.GridFSDBFile;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.http.CacheControl;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.concurrent.TimeUnit;

/**
 * Created by 骆清波 on 2017/11/16.
 */
@RestController
@RequestMapping("/assignment")
public class AssignmentController {
    @Autowired
    private AssignmentService asns;

    @Autowired
    AssignmentRepository asmr;

    @ApiOperation(value="submitWord", notes="老师上传任务书",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "teacherId",value = "老师id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "notes",value = "备注",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "teacherName",value = "老师名字",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicName",value = "课题id",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "word",value = "文件",dataType = "MultipartFile",paramType = "query"),
    } )
    @RequiresRoles({"teacher"})
    @RequestMapping("/submitWord")
    public Assignment updateUserWithSign(@RequestParam("notes")String notes, @RequestParam("teacherId")String teacherId, @RequestParam("teacherName")String teacherName,
                                         @RequestParam("topicName")String topicName, @RequestParam("topicId")String topicId,
                                         @RequestParam("word")MultipartFile word) throws IOException {
       return asns.submitWord(notes,teacherId,teacherName,topicId,topicName,word);

    }

    @ApiOperation(value="submitStuWord", notes="提交学生任务书",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "teacherId",value = "老师id",dataType = "String",paramType = "path"),
            @ApiImplicitParam(name = "notes",value = "备注",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "username",value = "学生学号",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "realname",value = "学生姓名",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "query"),
            @ApiImplicitParam(name = "word",value = "文件",dataType = "MultipartFile",paramType = "query"),
    } )
    @RequestMapping("/submitStuWord")
    public Assignment updateStuWord(@RequestParam("notes")String notes, @RequestParam("username")String username, @RequestParam("realname")String realname,
                                    @RequestParam("topicId")String topicId, @RequestParam("word")MultipartFile word) throws IOException {
        return asns.submitStuWord(notes,username,realname,topicId,word);

    }

    @ApiOperation(value="find", notes="根据课题查找任务书",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "topicId",value = "课题id",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/find/{topicId}")
    public Assignment find(@PathVariable String topicId){
        return asns.find(topicId);
    }

    @ApiOperation(value="downFile", notes="下载文件",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "resourceId",value = "文件id",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/downFile/{resourceId}")
    public ResponseEntity<Resource> downFile(@PathVariable String resourceId) throws UnsupportedEncodingException {
        GridFSDBFile file = asns.getFile(resourceId);
        String filename =  URLEncoder.encode(file.getFilename(), "UTF-8");
        HttpHeaders headers = new HttpHeaders();
        headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
        headers.add("Content-Disposition", String.format("attachment; filename=\"%s\"", filename));
        headers.add("Pragma", "no-cache");
        headers.add("Expires", "0");
        return ResponseEntity
                .ok()
                .headers(headers)
                .header(HttpHeaders.CONTENT_TYPE, file.getContentType())
                .header(HttpHeaders.CONTENT_ENCODING, "UTF-8")
                .cacheControl(CacheControl.maxAge(7, TimeUnit.DAYS))
                .contentType(MediaType.parseMediaType("application/octet-stream"))
                .body(new InputStreamResource(file.getInputStream()));
    }


    @ApiOperation(value="passAssignment", notes="通过课题",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/passAssignment/{username}")
    public Assignment passAssignment(@PathVariable String username){
        return asns.passAssignment(username);
    }


    @ApiOperation(value="depassAssignment", notes="撤销通过",httpMethod = "POST")
    @ApiImplicitParams(value ={
            @ApiImplicitParam(name = "username",value = "用户名",dataType = "String",paramType = "path")
    } )
    @RequestMapping("/depassAssignment/{username}")
    public Assignment depassAssignment(@PathVariable String username){
        return asns.depassAssignment(username);
    }
}
